package com.qf.lja.dao;

import com.qf.lja.enity.Product;
import com.qf.lja.enity.Type;
import com.qf.lja.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeDao {

    //查询指定类型的商品
    public Type getTypeProducts(int t_id)
    {
        String sql = "select * from type where type_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet rs = jdbcUtil.query(sql, t_id);
        try {
            if (rs.next()) {
                return convertToTypeProduct(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Type convertToTypeProduct(ResultSet rs) {
        try {
            Type type = new Type();
            type.setType_id(rs.getInt("type_id"));
            type.setType_name(rs.getString("type_name"));
            type.setType_info(rs.getString("type_info"));
            type.setType_url(rs.getString("type_url"));
            return type;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    //查询类型列表
    public List<Type> getTypeList()
    {
        String sql = "select * from type where 1=1";
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet rs = jdbcUtil.query(sql);

        List<Type> typeList = new ArrayList<>();

        try {
            while(rs.next()) {
               typeList.add(convertToTypeProduct(rs));
            }
            System.out.println("type list 查询成功 ！！！");
            return typeList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return typeList;
    }
}





